package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.BaseVo;
import com.moli.iov.admin.model.vo.req.stat.StatOnlineVo;
import com.moli.iov.admin.service.IStatFacadeService;
import com.moli.iov.his.model.dto.StatOnlineReqDTO;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

@Slf4j
@RestController
public class StatisticsController extends BaseController {

    @Resource
    IStatFacadeService statFacadeService;

    //在线率统计接口
    @PostMapping("/stat/stat/online")
    public RestResponse statOnline(@RequestBody StatOnlineReqDTO dto) {
        BaseVo baseVo = getBaseVo();
        Long companyId = null;
        if (UserTypeEnum.COMPANY.getFlag() == baseVo.getSysUserType()
                || UserTypeEnum.PLATFORM.getFlag() == baseVo.getSysUserType()) {
            companyId = baseVo.getSysCompanyId();
            if(UserTypeEnum.COMPANY.getFlag() == baseVo.getSysUserType()){
                dto.setCompanyId(companyId);
            }
        } else {
            return RestResponse.success();
        }

        RestResponse<StatOnlineVo> res = statFacadeService.getStatOnline(dto);
        return res;
    }

    //在线率统计接口
    @GetMapping("/stat/index/online")
    public RestResponse statOnline() {

        StatOnlineReqDTO dto = new StatOnlineReqDTO();
        dto.setBeginTime(getIndexBeginTime());

        BaseVo baseVo = getBaseVo();
        Long companyId = null;
        if (UserTypeEnum.COMPANY.getFlag() == baseVo.getSysUserType()
                || UserTypeEnum.PLATFORM.getFlag() == baseVo.getSysUserType()) {
            companyId = baseVo.getSysCompanyId();
            if(UserTypeEnum.COMPANY.getFlag() == baseVo.getSysUserType()){
                dto.setCompanyId(companyId);
            }
        } else {
            return RestResponse.success();
        }

        RestResponse<StatOnlineVo> res = statFacadeService.getStatOnline(dto);
        return res;
    }

    public Date getIndexBeginTime(){
        Date date = new Date();//获取当前时间    
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.add(Calendar.DATE, -15);
        return calendar.getTime();
    }

    /**
     * @param statType 统计类型 7：近7天 30：近30天 0：当天
     * @param alarmStatType 报警类型： 1:围栏报警，2：终端报警 3：风险预防报警
     * @return
     */
    @GetMapping("/stat/alarm")
    public RestResponse statFenceAlarm(Integer statType,Integer alarmStatType) {
        BaseVo baseVo = getBaseVo();
        RestResponse res = statFacadeService.getStatFenceAlarm(baseVo,statType,alarmStatType);
        return res;
    }

}
